﻿/// <reference path="_references.js" />

jQuery.fn.extend({
    updateVScrollToken: function (table) {

        var tableHasVScroll = table.hasVScrollBar();
        this.css("display", tableHasVScroll ? "" : "none");
    },

    hasVScrollBar: function () {
        return this.get(0).scrollHeight > this.innerHeight();
    },

    pointTopLeft: function () {
        var result = this.offset();
        return result;
    },

    pointTopRight: function () {
        var result = this.offset();
        result.left += this.width()
                + parseInt(this.css("padding-left"))
                + parseInt(this.css("padding-right"));
        return result;
    },

    pointBottomLeft: function () {
        var result = this.offset();
        result.top += this.height()
                + parseInt(this.css("padding-top"))
                + parseInt(this.css("padding-bottom"));
        return result;
    },

    pointBottomRight: function () {
        var result = this.offset();
        result.left += this.width()
                + parseInt(this.css("padding-left"))
                + parseInt(this.css("padding-right"));
        result.top += this.height()
                + parseInt(this.css("padding-top"))
                + parseInt(this.css("padding-bottom"));
        return result;
    },

    pointCenter: function () {
        var result = this.offset();
        result.left += (this.width() / 2)
                + parseInt(this.css("padding-left"))
                + parseInt(this.css("padding-right"));
        result.top += (this.height() / 2)
                + parseInt(this.css("padding-top"))
                + parseInt(this.css("padding-bottom"));
        return result;
    }
});

jQuery.extend({

    updateVScrollToken: function () {
        $('td.v_scroll_token').updateVScrollToken($('div.tbody'));
    },

    asoftAjax: function (data, action, controller, success, error, complete) {
        var url = controller ? '../' + controller + '/' + action : action;
        return $.ajax({
            type: 'Post',
            cache: false,
            dataType: 'json',
            url: url,
            data: JSON.stringify(data),
            contentType: 'application/json; charset=utf-8',
            async: true,
            success: success,
            error: error,
            complete: complete
        });
    },
    asoftAjaxTwoData: function (data1, data2, action, controller, success, error, complete) {
        var url = controller ? '../' + controller + '/' + action : action;
        return $.ajax({
            type: 'Post',
            cache: false,
            dataType: 'json',
            url: url,
            data: JSON.stringify({ data1: data1, data2: data2 }),
            contentType: 'application/json; charset=utf-8',
            async: true,
            success: success,
            error: error,
            complete: complete
        });
    }
});

// 総計の分（minute）を時(hour)に変換する
function ConvertTotalMinuteToHour(totalMinute) {
    var totalTime = Math.round(totalMinute);
    var Hours = Math.floor(totalTime / 60);
    var Minutes = totalTime % 60;
    if (Minutes < 10) {
        Minutes = "0" + Minutes;
    }
    
    timeString = Hours + ":" + Minutes;
    return timeString;
}

var numbericKeydown = function(event) {
    // 使用可能：Backspace、Delete、Tab、Esc、Enterキー
    if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
    // 使用可能：Ctrl+A
        (event.keyCode == 65 && event.ctrlKey === true) ||
        // 使用可能：Home、End、←、→
        (event.keyCode >= 35 && event.keyCode <= 39)) {
        // 実現させて、何も行わない
        return;
    } else {
        // これは数字のを確実し、キープレスを防ぐ
        if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
            event.preventDefault();
        }
    }
};

function PopupCenter(pageURL, title, w, h) {
    var left = (screen.width / 2) - (w / 2);
    var top = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
}

// URLの変数の値を取得する
function GetURLParameter(sParam) {
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}

$(document).ready(function () {
    var tabindex = 1;
    $('input,textarea,button,select,a').each(function () {
        if (this.type != "hidden") {
            $(this).attr("tabindex", tabindex++);
        }
    });
    $('.MasterFooter').find('input.first').attr('tabindex', tabindex++);
    $('.MasterFooter').find('input.second').attr('tabindex', tabindex++);
    $('.MasterFooter').find('input.last').attr('tabindex', tabindex++);
});

function calculatorDisasterStartTimeHourMinute(strDateTime, currentDateTimeServer) {
    var dateStart = Math.abs(Math.round(((new Date(currentDateTimeServer)) - new Date(strDateTime)) / 60000));
    LastDays = getDisasterStartTimeHourMinuteInDate(dateStart, 720);
    LastHours = getDisasterStartTimeHourMinuteInDate((dateStart - (LastDays * 720)), 60);
    LastMinutes = getDisasterStartTimeHourMinuteInDate((dateStart - (LastDays * 720) - (LastHours * 60)), 1);

    if (LastHours < 10) {
        LastHours = "0" + LastHours;
    }
    if (LastMinutes < 10) {
        LastMinutes = "0" + LastMinutes;
    }
    return LastHours + ":" + LastMinutes;
}

function getDisasterStartTimeHourMinuteInDate(secDiff, unitSeconds) {
    var tmp = Math.abs((tmp = secDiff / unitSeconds)) < 1 ? 0 : tmp;
    return Math.abs(tmp < 0 ? Math.ceil(tmp) : Math.floor(tmp));
}

function setTabIndex() {
    var tabindex = 1;
    $('input,select,textarea').each(function () {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);
            tabindex++;
        }
    });
}

function closeErrorPopup() {
    window.close();
    window.opener.location.reload();
}

function disButtonWhenProcessing() {
    var login = $('.login_button').val();
    if (login != null) {
        $('input[type=button]').attr('disabled', true);
        $('input[type=submit]').attr('disabled', true);
    }
    else {
        $('input[type=button]').attr('disabled', true);
        $('input[type=submit]').attr('disabled', true);
        
        $('input[type=button]').addClass("buttonDisable");
        $('input[type=submit]').addClass("buttonDisable");

        $('.link a').attr('onclick', '');
        $('.link a').attr('disabled', true);
        $('.link a').addClass("disBg active");
        $('input[type=button]').addClass("disBg");
        $('input[type=submit]').addClass("disBg");
    }
}

function enableButtonWhenProcessing() {
    var login = $('.login_button').val();
    if (login != null) {
        $('input[type=button]').removeAttr('disabled');
        $('input[type=submit]').removeAttr('disabled');
    }
    else {
        $('input[type=button]').removeAttr('disabled');
        $('input[type=submit]').removeAttr('disabled');

        $('.link a').attr('onclick', '');
        $('.link a').removeAttr('disabled');
        $('.link a').removeClass("disBg");

        $('input[type=button]').removeClass("disBg");
        $('input[type=submit]').removeClass("disBg");
        $('input[type=button]').removeClass("buttonDisable");
        $('input[type=submit]').removeClass("buttonDisable");
        $('input[type=button]').addClass("buttonNormal");
        $('input[type=submit]').addClass("buttonNormal");
    }
}

function checkBindTwoByte(e, value) {
    var textfield = value;
    var regex = /[^a-zA-Z0-9\_-]/gi;   
    if (regex.test(textfield.val())) {
        textfield.val(textfield.val().replace(regex, ""));
        alert(textfield.val());
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
   } else {
        var keyCode = (e.keyCode ? e.keyCode : e.which);
        if (keyCode <= 7 || keyCode >= 229) {
            alert($(this).val().length);
            var invalidChar = String.substr($(this).val().length - 1, 1);
            
            textfield.val(textfield.val().replace(invalidChar, ""));
            e.preventDefault();
            e.stopImmediatePropagation();
            return false;
        }
    }
}

//-----String.formatファンクション------
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined'
    ? args[number]
    : match
;
    });
};
